package com.sgffsg.learn.javalib.interview;

import java.io.File;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * @author: lmk
 * @date: 2024/4/24
 * @desc: 删除文件夹
 */
public class Test {

    public static boolean deletePath3(String path) {
        File file = new File(path);
        if (!file.exists()) {
            return true;
        }
        if (file.isDirectory()) {
            Stack<File> stack = new Stack<>();
            stack.push(file);
            while (!stack.isEmpty()) {
                File next = stack.peek();
                File[] files = next.listFiles();
                if (files.length == 0) {
                    //空文件夹
                    boolean deleted = deleteFile(next);
                    if (!deleted) {
                        return false;
                    }
                    stack.pop();
                } else {
                    //有文件，把所有的文件都删除，把所有的文件夹都入栈
                    for (int i = 0; i < files.length; i++) {
                        if (files[i].isDirectory()) {
                            stack.push(files[i]);
                        } else {
                            boolean deleted = deleteFile(file);
                            if (!deleted) {
                                return false;
                            }
                        }
                    }
                }

            }
        } else {
            return deleteFile(file);
        }
        return true;
    }

    public static boolean deletePath2(String path) {
        File file = new File(path);
        if (!file.exists()) {
            return true;
        }
        if (file.isDirectory()) {
            Queue<File> queue = new LinkedList<>();
            queue.offer(file);
            int index = 0;
            while (!queue.isEmpty()) {
                File next = queue.poll();
                File[] files = next.listFiles();
                for (int i = 0; i < files.length; i++) {
                    if (files[i].isDirectory()) {
                        queue.offer(files[index++]);
                    } else {
                        boolean deleted = deleteFile(file);
                        if (!deleted) {
                            return false;
                        }
                    }
                }
            }
        } else {
            return deleteFile(file);
        }
        return true;
    }

    public static boolean deletePath(String path) {
        File file = new File(path);
        if (file.isDirectory()) {
            return deleteDir(file);
        } else {
            return deleteFile(file);
        }
    }

    public static boolean deleteDir(File fileDir) {
        if (!fileDir.exists()) {
            return true;
        }
        File[] files = fileDir.listFiles();
        if (files.length == 0) {
            return fileDir.delete();
        }

        File temp;
        int index = 0;


        for (int i = 0; i < files.length; i++) {
            boolean deleted;
            if (files[i].isDirectory()) {
                deleted = deleteDir(files[i]);
            } else {
                deleted = deleteFile(files[i]);
            }
            if (!deleted) {
                return false;
            }
        }
        return true;
    }

    public static boolean deleteFile(File file) {
        if (!file.exists()) {
            return true;
        }
        return file.delete();
    }
}
